package assets_withdraw

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	user "gitee.com/liuxuezhan/ar-platform/rpc-client/loginClient/client"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/base-common/tool/wlog"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type WithdrawUnlockLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// b端财务管理页面--提现锁定取消
func NewWithdrawUnlockLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WithdrawUnlockLogic {
	return &WithdrawUnlockLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *WithdrawUnlockLogic) WithdrawUnlock(req *types.WithdrawUnlockReq) (resp *types.WithdrawUnlockRes, err error) {
	token, err := l.svcCtx.LoginRpc.ParseToken(l.ctx, &user.ParseTokenRequest{
		Token: headInfo.GetJwtToken(l.ctx),
	})
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}
	adminName := token.UserName
	rpc_req := &rpc.UnlockWithDrawReq{
		Locker:   adminName,
		OrderNos: req.Order_nos,
	}

	// 发送请求
	rpc_resp, err := l.svcCtx.AssetsRpc.PayWithdraw.WithdrawUnlock(l.ctx, rpc_req)
	if err != nil {
		wlog.Handle.Errorf(l.ctx, "Call ModifyVipLevelInfos error: %v", err)
		return nil, err
	}

	// 处理请求返回的结果
	// 解析、组合数据
	resp = &types.WithdrawUnlockRes{}
	if rpc_resp.Data != nil {
		for i, _ := range rpc_resp.Data {
			resp.Data.WithDraws = append(resp.Data.WithDraws, types.WithDraw{
				OrderNo: rpc_resp.Data[i].OrderNo,
				Status:  rpc_resp.Data[i].Status,
			})
		}

	}
	return
}
